package com.uetoken.cn.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.uetoken.cn.MainActivity;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.LanguageSelectAdapter;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.bean.LanguageSelectBean;
import com.uetoken.cn.language.LanguageType;
import com.uetoken.cn.language.MultiLanguageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 谢杨
 * @date 2018年7月31日14:09:35
 * 语言选择界面
 */
public class LanguageSelectActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView    mIvBack;
    @BindView(R.id.tv_title)
    TextView     mTvTitle;
    @BindView(R.id.ry_activity_language_select)
    RecyclerView mRyLanguageSelect;
    private LanguageSelectAdapter mAdapter;
    private List<LanguageSelectBean> mLanguageSelectBeans = new ArrayList<>();

    /**
     * 语言种类(3种)
     */
    private int languageType = 3;

    @Override
    public int getLayoutId() {
        return R.layout.activity_language_select;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        mTvTitle.setText(getString(R.string.str_language_select));
        mIvBack.setVisibility(View.VISIBLE);
        mRyLanguageSelect.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    @Override
    public void initData() {

        for (int i = 0; i < languageType; i++) {
            LanguageSelectBean languageSelectBean = new LanguageSelectBean();
            switch (i) {
                case 0:
                    languageSelectBean.setLanguage(getString(R.string.str_language_simplified_chinese));
                    languageSelectBean.setLanguageType(LanguageType.LANGUAGE_CHINESE_SIMPLIFIED);
                    break;
                case 1:
                    languageSelectBean.setLanguage(getString(R.string.str_language_traditional_chinese));
                    languageSelectBean.setLanguageType(LanguageType.LANGUAGE_CHINESE_TRADITIONAL);
                    break;
                case 2:
                    languageSelectBean.setLanguage(getString(R.string.str_language_english));
                    languageSelectBean.setLanguageType(LanguageType.LANGUAGE_EN);
                    break;
                default:
                    break;
            }
            mLanguageSelectBeans.add(languageSelectBean);
        }

        mAdapter = new LanguageSelectAdapter(R.layout.item_language_select, mLanguageSelectBeans);
        mRyLanguageSelect.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MultiLanguageUtil.getInstance().updateLanguage(mLanguageSelectBeans.get(position).getLanguageType());
                mAdapter.notifyDataSetChanged();
                Intent intent = new Intent(LanguageSelectActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void init() {

    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
